package org.jarraiders.musiclibrary.domain;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.annotations.Type;
import org.jarraiders.musiclibrary.shared.domain.TrackDTO;

/**
 * @author AlteroStudio
 */
@Entity
@Table(name = "albums", schema = "public")
public class Album implements Serializable {

	private static final long serialVersionUID = -546295151666398376L;

	@Id
	@Column(name = "al_id", columnDefinition = "uuid")
	@Type(type = "pg-uuid")
	@Basic(optional = false)
	private UUID albumId;

	@Column(name = "al_name")
	@Basic(optional = false)
	private String albumName;

	@Column(name = "al_year")
	private int albumYear;

	@Fetch(FetchMode.JOIN)
	@ManyToOne(optional = false, fetch = FetchType.EAGER)
	@JoinColumn(name = "artist_id", referencedColumnName = "ar_id")
	private Artist artist_id;

	@OneToMany(cascade = CascadeType.ALL, mappedBy = "album_id", fetch = FetchType.LAZY)
	private List<Track> tracksList;

	public Album(){
		
	}
	
	public Album(UUID albumId,String albumName,int albumYear,Artist artist){
		this.albumId=albumId;
		this.albumName=albumName;
		this.albumYear=albumYear;
		this.artist_id=artist;
	}
	
	public UUID getAlbumId() {
		return albumId;
	}

	public void setAlbumId(UUID albumId) {
		this.albumId = albumId;
	}

	public String getAlbumName() {
		return albumName;
	}

	public void setAlbumName(String albumName) {
		this.albumName = albumName;
	}

	public int getAlbumYear() {
		return albumYear;
	}

	public void setAlbumYear(int albumYear) {
		this.albumYear = albumYear;
	}

	public Artist getReferencedArtist() {
		return artist_id;
	}

	public void setReferencedArtist(Artist refferencedArtistId) {
		this.artist_id = refferencedArtistId;
	}

	public List<Track> getTracksList() {
		return tracksList;
	}
	
	/*public List<TrackDTO> getTracksDtoList() {
		List<TrackDTO> dtoList = new ArrayList<TrackDTO>(tracksList.size());
		
		for (Track track : tracksList) {
			dtoList.add(new TrackDTO(track));
		}
		
		return dtoList;
	}*/

	public void setTraksList(List<Track> traksList) {
		this.tracksList = traksList;
	}

	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("Album [albumId=");
		builder.append(albumId);
		builder.append(", albumName=");
		builder.append(albumName);
		builder.append(", albumYear=");
		builder.append(albumYear);
		builder.append(", referencedArtist=");
		builder.append(artist_id);
		builder.append(", tracksList=");
		builder.append(tracksList);
		builder.append("]");
		return builder.toString();
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((albumId == null) ? 0 : albumId.hashCode());
		result = prime * result
				+ ((albumName == null) ? 0 : albumName.hashCode());
		result = prime * result + albumYear;
		result = prime * result
				+ ((tracksList == null) ? 0 : tracksList.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Album other = (Album) obj;
		if (albumId == null) {
			if (other.albumId != null)
				return false;
		} else if (!albumId.equals(other.albumId))
			return false;
		if (albumName == null) {
			if (other.albumName != null)
				return false;
		} else if (!albumName.equals(other.albumName))
			return false;
		if (albumYear != other.albumYear)
			return false;
		if (tracksList == null) {
			if (other.tracksList != null)
				return false;
		} else if (!tracksList.equals(other.tracksList))
			return false;
		return true;
	}
}
